package io.renren.modules.jxgk.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.entity.StudentFeedbackEntity;
import io.renren.modules.jxgk.entity.StudentFeedbackRequset;
import io.renren.modules.jxgk.service.FeedBackService;
import io.renren.modules.jxgk.utils.ImageFile;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
@RestController
@RequestMapping("feedback/")
public class FeedbackController {

    @Autowired
    FeedBackService feedBackService;


    //上传照片
    @RequestMapping("UploadImage")
    public R upload(HttpServletRequest request,@Param("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");

        }
        //上传文件
       // String path= ImageFile.saveFile(request,file);
       // String thumbnailsPath=ImageFile.saveThumbnailsImage(request,file);
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        //保存文件信息
        String fileId= feedBackService.saveFile(path);
        return R.ok().put("data", fileId);
    }


    //根据username得到反馈信息
    @RequestMapping("getfeedbacksList")
    @ResponseBody
    public R getfeedbackList(
             StudentFeedbackRequset studentFeedbackRequset
    )
    {
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        studentFeedbackRequset.setUsername(username);
        List<StudentFeedbackEntity> studentFeedbackEntityList=null;
        try {
            studentFeedbackEntityList=feedBackService.getfeedbackList(studentFeedbackRequset);
        }catch (Exception e)
        {
            System.out.printf(studentFeedbackRequset.toString());
        }
        return R.ok().put("data",studentFeedbackEntityList);
    }


    //查看所有反馈信息
    @RequestMapping("/getalllist")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<StudentFeedbackEntity> configList = feedBackService.getAllList(query);
        int total = feedBackService.getTotal(query);

        PageUtils pageUtil = new PageUtils(configList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    //增加反馈
    @RequestMapping("addfeedback")
    @ResponseBody
    public R addfeedback( HttpServletRequest request,
            @RequestBody StudentFeedbackEntity StudentFeedbackEntity
    )
    {
        //用户名
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        StudentFeedbackEntity.setUsername(username);
        try{
            feedBackService.saveStudentFeedbackEntity(StudentFeedbackEntity);
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.ok().put("success", false);
        }
         return R.ok().put("success", true);
    }

//删除反馈
    @RequestMapping("deletefeedback")
    @ResponseBody
    public R deletefeedback( String id) throws Exception {

        try
        {
            feedBackService.deletefeedback(id);
        }catch(Exception e)
        {
            e.printStackTrace();
            return R.ok().put("success", false);

        }


        return R.ok().put("success", true);
    }
}
